package com.rattsoftware.client.views;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.rattsoftware.client.AppMessages;
import com.rattsoftware.client.controllers.LoginController;

public class LoginView extends View {
	
	AppMessages appMessages = GWT.create(AppMessages.class);
    private Dialog loginWindow;
    protected Status status;
    
    protected TextField<String> username;
	protected TextField<String> password;
	protected Button btnLogin;
	protected Button reset;

	public LoginView(Controller controller) {
		super(controller);
	}

	protected void initialize() {
		
		FormLayout layout = new FormLayout();
		layout.setLabelWidth(90);
		layout.setDefaultWidth(155);
		
		loginWindow = new Dialog();
        //loginWindow.setModal(true);
        loginWindow.setWidth("300");
        loginWindow.setClosable(false);
        loginWindow.setResizable(false);
        loginWindow.setDraggable(false);
        loginWindow.setPlain(false);
        loginWindow.setHeading("Ratts Login");
        loginWindow.setBodyStyle("padding: 8px;background: none");
        loginWindow.setBodyBorder(true);
        
        loginWindow.setLayout(layout);

        loginWindow.setButtonAlign(HorizontalAlignment.LEFT);
        loginWindow.setButtons("");
        loginWindow.setIcon(IconHelper.createStyle("user"));
        
        KeyListener keyListener = new KeyListener() {
			public void componentKeyUp(ComponentEvent event) {
				validate();
			}
		};
    
        username = new TextField<String>();
        username.setFieldLabel(appMessages.username());
        username.setMinLength(4);
        username.setAllowBlank(false);
        username.addKeyListener(keyListener);
        loginWindow.add(username);

        password = new TextField<String>();
        password.setMinLength(4);
        password.setFieldLabel(appMessages.passWord());
        password.setAllowBlank(false);
        password.addKeyListener(keyListener);
        password.setPassword(true);
        loginWindow.add(password);
        
        loginWindow.setFocusWidget(username);
        
        status = new Status();
		status.setBusy("please wait...");
		status.hide();
		status.setAutoWidth(true);
		loginWindow.getButtonBar().add(status);
		
		loginWindow.getButtonBar().add(new FillToolItem());

        btnLogin = new Button(appMessages.login());
        btnLogin.setType("submit");
        btnLogin.disable();
        btnLogin.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent ce) {
            	status.show();
            	loginWindow.getButtonBar().disable();
            	
            	Timer t = new Timer() {

        			@Override
        			public void run() {
        				loginWindow.hide();
        			}
        		};
        		t.schedule(2000);
            	
                ((LoginController) controller).doLogin(
                        username.getValue(), password.getValue());
            }
        });
        loginWindow.addButton(btnLogin);

        // reset button
        reset = new Button("Reset");
        reset.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				username.reset();
				password.reset();
				validate();
				username.focus();
			}

		});
        
        loginWindow.addButton(reset);
	}
	
	public void showWindow() {
        loginWindow.show();
    }

    public void closeWindow() {
        loginWindow.hide();
    }

    @Override
    protected void handleEvent(AppEvent event) {
        loginWindow.show();
    }
    
    protected void validate() {
    	btnLogin.setEnabled(hasValue(username) && hasValue(password)
				&& password.getValue().length() > 3);
	}
    
    protected boolean hasValue(TextField<String> field) {
		return field.getValue() != null && field.getValue().length() > 0;
	}
}